package com.mvc.dao;

import com.mvc.entity.Department;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by zhudong on 2016/9/5.
 */
@Service
@Transactional
public class DepartmentDao extends BaseDao{
//public class DepartmentDao{

//    @Autowired
//    private SessionFactory sessionFactory;
//
//    protected Session getSession(){
//        return sessionFactory.getCurrentSession();
//    }

    public List<Department> getAll(){
        Session session = getSession();
        String hql = "from Department";
        return session.createQuery(hql).list();
    }

    public Department getById(Integer id){
        Session session = getSession();
        return (Department)session.get(Department.class,id);
    }

    public void save(Department department){
        Session session = getSession();
        session.saveOrUpdate(department);
    }

    public void delete(Integer id){
        Session session = getSession();
        String hql = "from Department d where d.id=:id";
        session.createQuery(hql).setInteger("id",id).executeUpdate();
    }

}
